package com.mvu.banana.domain.finder;

import com.mvu.banana.domain.server.BlockReturn;
import com.mvu.banana.domain.server.EM;
import com.mvu.banana.domain.stub.Credential;

import javax.persistence.EntityManager;
import java.util.List;

public class CredentialFinder {
  public static Credential find(final String username) {
    return new BlockReturn<Credential>(){
      @Override
      public Credential run(EntityManager em) {
        List resultList = em.createQuery("from Credential c where c.username = :username")
                .setParameter("username", username)
                .setMaxResults(1)
                .getResultList();
        return resultList.isEmpty() ? null : (Credential) resultList.get(0);
      }
    }.run();
  }

  public static Credential findById(Long credentialId) {
    return EM.find(Credential.class, credentialId);
  }
}
